package com.rjt.qrcode.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.rjt.qrcode.model.User;
import com.rjt.qrcode.util.QRCodeUtil;

/**
 * @author 7
 * @create 2019/1/22
 * @since 1.0.0
 */
@Controller
@RequestMapping("qrcode")
public class QrcodeController {
    @Autowired
    private StringRedisTemplate sringRedisTemple;
    
    public static Map<String, String> userMap;

    @GetMapping("index")
    public String index() {
        return "qrcode";
    }

    @PostMapping("get_qrcode")
    @ResponseBody
    public String getQrcode(HttpServletRequest req, HttpServletResponse resp) {
        String uuid = UUID.randomUUID().toString();
        String base64 = QRCodeUtil.toBase64("http://106.12.200.36/qrcode/check_uuid?uuid=" + uuid, 500, 300);
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uuid", uuid);
        hashMap.put("base64", base64);
        String result = JSONObject.toJSONString(hashMap);
        //目前UUID对应的用户是空的，手机扫描后往这个uuid对应的user放入相应的cookie或者登录信息，这样轮询的时候就能拿到这些登录信息
        sringRedisTemple.opsForValue().set(uuid, "no_login",60, TimeUnit.SECONDS);
        return result;
    }

    @PostMapping("roll_poling")
    @ResponseBody
    public String rollPoling(HttpServletRequest req, HttpServletResponse resp, String uuid) {
        try {
            Thread.sleep(2000); //轮询操作不要太快，休息两秒钟
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(!sringRedisTemple.hasKey(uuid)){
            return "no_exist";
        }
        //浏览器传来uuid
        String userStr = sringRedisTemple.opsForValue().get(uuid);
        if ("no_login".equals(userStr)) {
            return "fail";
        }
        JSONObject jsonObject = JSONObject.parseObject(userStr);

        if (jsonObject != null) {
            //user不为空说明扫描登录成功，做相应的处理即可
            System.out.println(jsonObject.get("name"));
            return "success";
        }
        return "error";
    }

    @GetMapping("check_uuid")
    @ResponseBody
    public String checkUuid(HttpServletRequest req, HttpServletResponse resp, String uuid) {
        User u = new User();
        u.setId(8888);
        u.setName("tanran");
        if(!sringRedisTemple.hasKey(uuid)){
          return "二维码已过期，请刷新重新扫码";
        }
        sringRedisTemple.opsForValue().set(uuid, JSONObject.toJSONString(u),60, TimeUnit.SECONDS);
        return "扫码成功";
    }
   /* @GetMapping("test")
    public String test() {
    	redisTemple.opsForZSet().add(key, value, score)
    }*/
   
    
}